package com.fuluwa.domain.product.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FieldStatsInfo;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.RangeFacet;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.HighlightParams;

import com.ctc.wstx.util.StringUtil;
import com.fuluwa.infra.lucene.LuceneServiceImpl;

public class FacetQueryService {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
new FacetQueryService().facetQuery("格式","计算机");
	}
	public FacetResult facetQuery(String queryString,String facaeQueryString) throws Exception {
		SolrQuery query = new SolrQuery();
		query.setStart(0);
		query.setRows(10);
		if(StringUtils.isNotBlank(queryString))
		query.setQuery(queryString);
//		query.set("qf","");
//		for (Entry<String, ORDER> order : q.getOrder().entrySet()) {
//			query.addSortField(order.getKey(), order.getValue());
//		}
		query.setFields("id");//only return id
		
		query.setFacet(true);
		query.addFacetField("product_category");
		query.addFacetField("author_name");
		query.addFacetField("product_keywords");
		query.addFacetPivotField("author_name,product_keywords,product_category");
//		query.addFacetPivotField("product_keywords");
//		query.addFacetPivotField("product_category");
		query.addNumericRangeFacet("product_price", 0, 1000, 5);
		query.setFacetMinCount(1);
		if(StringUtils.isNotBlank(facaeQueryString))
		query.addFacetQuery(facaeQueryString);
//		query.addFacetQuery("计算机");
//		query.addFacetQuery("财付通");
	
		
		query.setHighlight(true);
		query.setHighlightSimplePre("<font color=\"red\">");
		query.setHighlightSimplePost("</font>");
		query.setHighlightFragsize(0);
		query.addHighlightField("product_name");
		query.addHighlightField("file_content");
		query.addHighlightField("product_introduction");
		query.set(HighlightParams.ALTERNATE_FIELD, "product_name");


		QueryResponse rsp = new LuceneServiceImpl().getSolrServer().query(query);
		
		FacetResult ret=new FacetResult();
		
		ret.setDocumentList(rsp.getResults());
		ret.setFacetFields(rsp.getFacetFields());
		ret.setFacetRanges(rsp.getFacetRanges());
		ret.setHighlighting(rsp.getHighlighting());
		
		/*System.out.println(rsp);
		rsp.getResults().getNumFound();
		//rsp.getResponse();
		
		List<FacetField> facetFields = rsp.getFacetFields();
		List<RangeFacet> facetRanges = rsp.getFacetRanges();
		Map<String, Map<String, List<String>>> highlighting = rsp.getHighlighting();
		Map<String, FieldStatsInfo> fieldStatsInfo = rsp.getFieldStatsInfo();

		Iterator<SolrDocument> iter = rsp.getResults().iterator();*/
		//logger.info(rsp.getRequestUrl());
		return ret;
	}
}


